package com.bacel.nb.product.core.service.impl;

import com.bacel.nb.product.core.dao.Dao;
import com.bacel.nb.product.core.dao.IDevicedataDao;
import com.bacel.nb.product.core.domain.Devicedata;
import com.bacel.nb.product.core.dto.ListGageModule;
import com.bacel.nb.product.core.service.IDevicedataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Bacel on 2017/05/02.
 */
@Service
public class DevicedataService extends BaseServiceImpl<Devicedata> implements IDevicedataService {

    @Autowired
    private IDevicedataDao moduleDao;

    @Override
    protected Dao getDao() {
        return moduleDao;
    }

    @Override
    public void getPageByExampleEq(Devicedata query, ListGageModule<Devicedata> page) {
        List<Devicedata> list = this.getDao().findByExampleEq(query, page.getPageindex(), page.getPagesize());
        long count = this.getDao().countByExampleEq(query);
        for (Devicedata devicedata : list) {
            String data=getdataString(devicedata.getMetadata());
            devicedata.setDataString(data);
        }
        page.setPagecount((int) count / page.getPagesize() + 1);
        page.setList(list);
        page.setTotalcount((int) count);
    }

    private static String getdataString(byte[] bytes){
        StringBuffer stringBuffer=new StringBuffer();
        for (byte b:bytes) {
            stringBuffer.append((char) b);
        }
        return stringBuffer.toString();
    }

}